<?php

namespace app\api\controller;

use think\Controller;
use app\api\controller\Base;
use think\Request;
Use \app\api\model\Vacate as VacateModel;

class Vacate extends Controller {
    protected $middleware = ['Auth'];
    /**
     * 请假申请
     * @param Request $request
     * @return \think\response\Json
     */
    public function apply(Request $request)
    {
        if (!request()->isPost()) {
            return fail(INVALID_REQUEST, '无效的请求');
        }
        $scheduleIdId = input('?post.schedule_id') ? input('post.schedule_id') : 0;
        $reason = input('post.reason');//请假原因
        if (!$scheduleIdId) {
            return fail(INVALID_PARAMS, '请选择需要的请假的课程');
        }
        $userId = $request->userId;
        $result = VacateModel::doVacate($userId,$scheduleIdId,$reason);
        if($result){
            return  success($result);
        }else{
            return  fail(DATA_NOT_EXIST, '申请失败');
        }

    }

    /**
     * 申请请假列表
     */
    public function vacateList(Request $request){
        $groupid = $request->groupId;
        $userid = $request->userId;
        $result = VacateModel::queryAllVacateList($groupid,$userid);
        if($result){
            return  success($result);
        }else{
            return  fail(DATA_NOT_EXIST, '查询失败');
        }
    }

    /**
     * 根据id查询假期内容
     */
    public function vacateInfo(){
        $vacateid = input('?get.vacate_id') ? input('get.vacate_id') : 0;
        if(!$vacateid){
            return  fail(INVALID_REQUEST, '参数错误');
        }
        $result = VacateModel::queryVacateById($vacateid);
        if($result){
            return  success($result);
        }else{
            return  fail(DATA_NOT_EXIST, '查询失败');
        }
    }

    /**
     * 教师审核假期
     */
    public function auditVacate(Request $request){
        $groupid = $request->groupId;
        $userid = $request->userId;
        if($groupid == 10001){
            return fail(INVALID_REQUEST, '无该权限');
        }
        $vacateid = input('?post.vacate_id') ? input('post.vacate_id') : 0;
        if(!$vacateid){
            return fail(INVALID_REQUEST, '参数错误');
        }
        $status = input('?post.status') ? input('post.status') : 0;
        $teacherOpinion = input('?post.teacher_opinion') ? input('post.teacher_opinion') : '';
        $result = VacateModel::auditVacate($vacateid,$status,$teacherOpinion);
        if($result){
            return  success($result);
        }else{
            return  fail(DATA_NOT_EXIST, '操作失败');
        }

    }

}